﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web;

    public abstract class MakeThumbnail : IHttpHandler
    {
        protected MakeThumbnail()
        {
        }

        private string GetContentType(ImageFormat iFormat)
        {
            if (!iFormat.Guid.Equals(ImageFormat.Jpeg.Guid))
            {
                if (iFormat.Guid.Equals(ImageFormat.Gif.Guid))
                {
                    return "image/gif";
                }
                if (iFormat.Guid.Equals(ImageFormat.Png.Guid))
                {
                    return "image/png";
                }
                if (iFormat.Guid.Equals(ImageFormat.Tiff.Guid))
                {
                    return "image/tiff";
                }
                if (iFormat.Guid.Equals(ImageFormat.Bmp.Guid))
                {
                    return "image/x-ms-bmp";
                }
            }
            return "image/jpeg";
        }

        public void ProcessRequest(HttpContext context)
        {
            ImageFormat iFormat = null;
            int num3 = 0;
            int num2 = 0;
            string cacheKey = "";
            Bitmap image = null;
            Bitmap bitmap = null;
            Bitmap bitmap2 = null;
            HttpRequest request = context.Request;
            string str = "";
            int num = -1;
            if (request.Params["w"] != string.Empty)
            {
                num3 = int.Parse(request.QueryString["w"]);
            }
            if (request.Params["h"] != string.Empty)
            {
                num2 = int.Parse(request.QueryString["h"]);
            }
            if (request.Params["p"] != string.Empty)
            {
                num = int.Parse(request.QueryString["p"]);
            }
            if (num2 != 0)
            {
                if (num2 > num3)
                {
                    str = "P";
                }
                else if (num2 == num3)
                {
                    str = "S";
                }
                else
                {
                    str = "L";
                }
            }
            else
            {
                str = "";
                num2 = num3;
            }
            string path = "";
            string homeDirectoryMapPath = "";
            homeDirectoryMapPath = PortalController.GetCurrentPortalSettings().HomeDirectoryMapPath;
            if (!Versioned.IsNumeric(request.Params["Image"]))
            {
                path = homeDirectoryMapPath + request.Params["Image"];
                if (!File.Exists(path))
                {
                    path = homeDirectoryMapPath + "noimage.png";
                }
                cacheKey = path.Substring(path.LastIndexOf(@"\Portals\")) + Conversions.ToString(num3) + "x" + Conversions.ToString(num2);
            }
            else
            {
                cacheKey = "NB_Store" + request.Params["Image"] + "." + Conversions.ToString(num3) + "x" + Conversions.ToString(num2);
                if (DataCache.GetCache(cacheKey) == null)
                {
                    NB_Store_ProductImageInfo productImage = new ProductController().GetProductImage(Conversions.ToInteger(request.Params["Image"]), "");
                    if (productImage == null)
                    {
                        path = homeDirectoryMapPath + "noimage.png";
                    }
                    else
                    {
                        path = productImage.ImagePath;
                    }
                }
            }
            if (DataCache.GetCache(cacheKey) != null)
            {
                bitmap2 = (Bitmap) ((Bitmap) DataCache.GetCache(cacheKey)).Clone();
                iFormat = ImageFormat.Jpeg;
            }
            else if (File.Exists(path))
            {
                image = new Bitmap(path);
                iFormat = ImageFormat.Jpeg;
                int width = image.Width;
                int height = image.Height;
                if (!(((num3 > 0) & (num3 < width)) | ((num2 > 0) & (num2 < height))))
                {
                    bitmap2 = image;
                }
                else
                {
                    int num7;
                    int num8;
                    double num6 = image.PhysicalDimension.Width / image.PhysicalDimension.Height;
                    if (image.PhysicalDimension.Height < image.PhysicalDimension.Width)
                    {
                        num8 = num3;
                        num7 = (int) Math.Round((double) (((double) num3) / num6));
                    }
                    else
                    {
                        num8 = (int) Math.Round((double) (num3 * num6));
                        num7 = num3;
                    }
                    switch (str)
                    {
                        case "":
                        {
                            bitmap2 = new Bitmap(num8, num7);
                            Graphics graphics = Graphics.FromImage(bitmap2);
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics.FillRectangle(Brushes.White, 0, 0, num8, num7);
                            graphics.DrawImage(image, 0, 0, num8, num7);
                            break;
                        }
                        case "L":
                        {
                            num8 = num3;
                            num7 = (int) Math.Round((double) (((double) num3) / num6));
                            bitmap = new Bitmap(num8, num7);
                            Graphics graphics3 = Graphics.FromImage(bitmap);
                            graphics3.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics3.FillRectangle(Brushes.White, 0, 0, num8, num7);
                            graphics3.DrawImage(image, 0, 0, num8, num7);
                            Rectangle destRect = new Rectangle(0, 0, num8, num2);
                            bitmap2 = new Bitmap(num8, num2);
                            Graphics graphics2 = Graphics.FromImage(bitmap2);
                            graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics2.FillRectangle(Brushes.White, 0, 0, num8, num2);
                            graphics2.DrawImage(bitmap, destRect, 0, (int) Math.Round((double) (((double) (bitmap.Height - num2)) / 2.0)), num8, num2, GraphicsUnit.Pixel);
                            break;
                        }
                        case "P":
                        {
                            num8 = (int) Math.Round((double) (num2 * num6));
                            num7 = num2;
                            bitmap = new Bitmap(num8, num7);
                            Graphics graphics5 = Graphics.FromImage(bitmap);
                            graphics5.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics5.FillRectangle(Brushes.White, 0, 0, num8, num7);
                            graphics5.DrawImage(image, 0, 0, num8, num7);
                            Rectangle rectangle2 = new Rectangle(0, 0, num3, num7);
                            bitmap2 = new Bitmap(num3, num7);
                            Graphics graphics4 = Graphics.FromImage(bitmap2);
                            graphics4.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics4.FillRectangle(Brushes.White, 0, 0, num3, num7);
                            graphics4.DrawImage(bitmap, rectangle2, (int) Math.Round((double) (((double) (bitmap.Width - num3)) / 2.0)), 0, num3, num7, GraphicsUnit.Pixel);
                            break;
                        }
                        default:
                            if (str == "S")
                            {
                                if (image.PhysicalDimension.Height < image.PhysicalDimension.Width)
                                {
                                    num8 = (int) Math.Round((double) (num2 * num6));
                                    num7 = num2;
                                }
                                else
                                {
                                    num8 = num3;
                                    num7 = (int) Math.Round((double) (((double) num3) / num6));
                                }
                                bitmap = new Bitmap(num8, num7);
                                Graphics graphics7 = Graphics.FromImage(bitmap);
                                graphics7.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics7.FillRectangle(Brushes.White, 0, 0, num8, num7);
                                graphics7.DrawImage(image, 0, 0, num8, num7);
                                Rectangle rectangle3 = new Rectangle(0, 0, num3, num2);
                                bitmap2 = new Bitmap(num3, num2);
                                Graphics graphics6 = Graphics.FromImage(bitmap2);
                                graphics6.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics6.FillRectangle(Brushes.White, 0, 0, num3, num2);
                                graphics6.DrawImage(bitmap, rectangle3, 0, (int) Math.Round((double) (((double) (bitmap.Height - num2)) / 2.0)), num3, num2, GraphicsUnit.Pixel);
                            }
                            break;
                    }
                    DataCache.SetCache(cacheKey, RuntimeHelpers.GetObjectValue(bitmap2.Clone()));
                }
            }
            if ((iFormat != null) & (bitmap2 != null))
            {
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 0x55L);
                context.Response.ContentType = this.GetContentType(iFormat);
                bitmap2.Save(context.Response.OutputStream, imageEncoders[1], encoderParams);
                bitmap2.Dispose();
            }
            if (image != null)
            {
                image.Dispose();
            }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        
    }
}

